//
//  sanQuYuViewController.m
//  sanFsports
//
//  Created by mac on 16/11/21.
//  Copyright © 2016年 sanAiFuSport.com. All rights reserved.
//

#import "sanQuYuViewController.h"
#import "Masonry.h"
// cell的重用标识符
static NSString *cellId = @"cellId";

// cell的重用标识符
static NSString *sanLeftCell = @"sanLeftCell";
static NSString *sanDetailCell = @"sanDetailCell";
@interface sanQuYuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak) UITableView *detailtv;
@property(nonatomic,weak) UITableView *lefttv;

@property(nonatomic,assign)BOOL isPullup;
@property(nonatomic,assign)CGFloat lastOffSetY;

@end

@implementation sanQuYuViewController
{
    NSArray<NSString *> *_cityList;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
}

#pragma mark-- 搭建UI界面
- (void)setupUI
{
     _cityList = @[@"河北",@"北京",@"黑龙江",@"吉林",@"辽宁",@"内蒙古",@"新疆",@"河南",@"山西",@"山东"];
    
    self.view.backgroundColor = [UIColor orangeColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
    btn.frame = CGRectMake(0, 0, 50, 50);
    
    [btn addTarget:self action:@selector(dismisVc) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btn];
    
    // MARK: - 1.左侧的分类列表视图
    UITableView *lefttv = [[UITableView alloc] init];
    
    [self.view addSubview:lefttv];
    
    [lefttv mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(80);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(90);
        make.bottom.equalTo(self.view);
        
    }];
    //2 侧有视图
    UITableView *detailvc = [[UITableView alloc]init];
    [self.view addSubview:detailvc];
    
    [detailvc mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(lefttv);
        make.right.equalTo(self.view);
        make.left.equalTo(lefttv.mas_right);
        
    }];
    //设置一下行高
    lefttv.rowHeight = 50;
    
    //设置预估的行高
    detailvc.rowHeight = 325;
    // MARK: -----设置列表的属性-------
    // 1.设置数据源和代理对象
    lefttv.dataSource = self;
    lefttv.delegate = self;
    
    detailvc.dataSource = self;
    detailvc.delegate = self;
    
    //注册Cell
    [lefttv registerClass:[UITableViewCell class] forCellReuseIdentifier:sanLeftCell];
    [detailvc registerNib:[UINib nibWithNibName:@"SanCategoryView" bundle:nil] forCellReuseIdentifier:sanDetailCell];
    
    //不显示多余的行数
    lefttv.tableFooterView = [[UIView alloc]init];
    detailvc.tableFooterView = [[UIView alloc]init];
    
    //记录成员变量
    self.detailtv = detailvc;
    self.lefttv = lefttv;

}
#pragma mark-- 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.lefttv) {
        return 1;
    }
    return _cityList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.lefttv) {
        return _cityList.count;
    }
    return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    if (tableView == self.lefttv) {
        cell = [tableView dequeueReusableCellWithIdentifier:sanLeftCell forIndexPath:indexPath];
        cell.textLabel.text = _cityList[indexPath.row];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:sanDetailCell forIndexPath:indexPath];
    }
    return cell;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (tableView == self.detailtv) {
        return @"张家口崇礼县";
    }
    return nil;
}

- (void)dismisVc
{
    // 销毁控制器 dismiss!
    [self dismissViewControllerAnimated:YES completion:^{
        
        NSLog(@"销毁");
    }];
    
}
#pragma mark -- 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.detailtv) {
        if (scrollView.contentOffset.y > _lastOffSetY) {
            _isPullup = true;
        } else {
            _isPullup = false;
        }
        _lastOffSetY = scrollView.contentOffset.y;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.lefttv) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        [_detailtv scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (tableView == self.detailtv) {
        if (_isPullup && (tableView.isDecelerating || tableView.isDragging || tableView.isTracking)) {
            
            [_lefttv selectRowAtIndexPath:[NSIndexPath indexPathForRow:section + 1 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (tableView == self.detailtv) {
        if (!_isPullup && (tableView.isDecelerating || tableView.isDragging || tableView.isTracking)) {
            
            [_lefttv selectRowAtIndexPath:[NSIndexPath indexPathForRow:section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
    

}
@end
